from db import db_handler
#解冻用户账户
def thawing_account_interface(thawing_name):
    user_dic=db_handler.select(thawing_name)
    if user_dic:
        if user_dic['lock']:
            user_dic['error_login']=0
            user_dic['lock']=False
            db_handler.save(user_dic)
            return True,'%s 用户已解冻，稍后请该用户登陆再次确认.'%thawing_name
        else:
            return False,'%s 用户尚未冻结，请核对后重试'%thawing_name
    else:
        return False,'%s 用户不存在,请核对后重试.'%thawing_name

#冻结用户账户
def freeze_account_interface(freeze_name):
    user_dic = db_handler.select(freeze_name)
    if user_dic:
        if not user_dic['lock']:
            user_dic['error_login'] =3
            user_dic['lock'] = True
            db_handler.save(user_dic)
            return True, '冻结 %s 成功！'% freeze_name
        else:
            return False, '%s 用户已冻结，请核对后重试'% freeze_name
    else:
        return False, '%s 用户不存在,请核对后重试.'%freeze_name




#提升用户额度
def increase_credit_interface(name,credit):
    user_dic=db_handler.select(name)
    if user_dic:
        user_dic['credit']+=credit
        db_handler.save(user_dic)

        return True,'提升 %s 额度成功!'%name

    else:
        return False,'提升额度的用户不存在，请核对后重试...'



#减少用户额度
def decrease_credit_interface(name,credit):
    user_dic = db_handler.select(name)
    if user_dic:
        if  user_dic['credit']>credit:
            user_dic['credit'] -= credit

            db_handler.save(user_dic)
            return True, '降低 %s 额度成功,额度为:%s!' %(name,user_dic['credit'])
        else:
            return False,'该用户额度为:%s,额度减少要小于信用额度.'%name

    else:
        return False, '降低额度的用户不存在，请核对后重试...'

#重置用户的密码
def reset_password_interface(reset_name,reset_password):
    user_dic=db_handler.select(reset_name)
    if user_dic:
        user_dic['password']=reset_password
        db_handler.save(user_dic)
        return True,'重置 %s 密码成功!'%reset_name
    else:
        return False,'该用户不存在，请核对后重试...'




#添加新用户
#调用user_interface接口添加用户
